package com.kkwrite.regimen.provider.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.kkwrite.regimen.provider.domain.RegActivityApplyDO;

/**
 *
 * @author jinying
 * 
 */
public interface RegActivityApplyRepository extends
		JpaRepository<RegActivityApplyDO, Long> {
	
	/**
     * 根据活动编号查询报名表中的报名信息
     * @param activityCode 活动编号
     * 
     */
	List<RegActivityApplyDO> findAllByActivityCode(String activityCode);
	
	/**
     * 根据活动编号用户Id查询活动报名信息
     * @param activityCode 活动编号，userId用户编号
     * 
     */
	RegActivityApplyDO findOneByActivityCodeAndUserId(String activityCode, Long userId);

	/**
     * 根据订单编号查询活动报名信息
     * @param orderCode 订单编号
     * 
     */
	RegActivityApplyDO findOneByOrderCode(String orderCode);
	
	/**
     * 根据活动编号，查询该活动有多少有效的报名数量（支付成功算有效报名）
     * 
     */
	
	@Query(value="SELECT count(1) FROM reg_activity_apply t LEFT JOIN reg_order t1 ON t.order_code = t1.order_code WHERE t1.pay_status = 1 AND t.activity_code = :activityCode", nativeQuery=true)
	int getEffectiveActivityApplyCount(@Param("activityCode") String activityCode);
}
